import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import sysLayout from '../views/system/layout/index.vue'
import sysUser from '../views/system/user/index.vue'
import sysMenu from '../views/system/menu/index.vue'
import sysDept from '../views/system/dept/index.vue'
import sysRole from '../views/system/role/index.vue'
import sysJob from '../views/system/job/index.vue'
import operLog from '../views/system/log/operLog.vue'
import loginLog from '../views/system/log/loginLog.vue'
import login from '../views/login.vue'
import plApp from '../views/platform/app/index.vue'
import plInterface from '../views/platform/interface/index.vue'
import plInterfaceDetail from '../views/platform/interface/detail.vue'

import { getAuthorization } from '@/utils/auth'
import { message } from 'ant-design-vue'
import store from '@/store/index'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: "sys/role"
    },
    {
        path: '/login',
        name: "login",
        component: login,
    },
    {
        path: '/sys',
        name: 'sys',
        component: sysLayout,
        children: [
            {
                path: 'user',
                name: 'sysUser',
                component: sysUser,
            },
            {
                path: 'menu',
                name: 'sysMenu',
                component: sysMenu,
            },
            {
                path: 'dept',
                name: 'sysDept',
                component: sysDept,
            },
            {
                path: 'role',
                name: 'role',
                component: sysRole,
            },
            {
                path: 'job',
                name: 'sysJob',
                component: sysJob,
            },
            {
                path: 'operlog',
                name: 'sysOperlog',
                component: operLog,
            },
            {
                path: 'loginlog',
                name: 'sysLoginlog',
                component: loginLog,
            },
        ]
    },
    {
        path: '/pl',
        name: 'pl',
        component: sysLayout,
        children: [
            {
                path: 'app',
                name: 'plApp',
                component: plApp,
            },
            {
                path: 'interface',
                name: 'plInterface',
                component: plInterface,
            },
            {
                path: 'interfaceDetail',
                name: 'plInterfaceDetail',
                component: plInterfaceDetail,
            }
        ]
    }
]



const defaultRouters = [
    {
        path: '/',
        redirect: "/dashboard/home",
    },
    {
        path: '/login',
        name: "login",
        component: () => import("@/views/login.vue"),
        meta: {},
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes: defaultRouters
})


/**
 * 异步路由
 */
const loadRouters = (menus: Array<any>) => {
    // 添加路由信息
    if (menus) {
        menus.forEach((item: any) => {
            let route = {
                path: item.type == 'folder' ? '/' + item.path : item.path,
                name: item.componentName,
                component: () => import(`@/views${item.componentPath}`),
                redirect: item.redirect,
                meta: {
                    name: item.name,
                    icon: item.icon,
                    type: item.type
                },
            }
            if (item.children && item.children.length > 0) {
                recursion(item.children, route)
            }
            router.addRoute(route)
        })
    }
}

/**迭代目录 */
// childMenus route
const recursion = (menus: Array<any>, route: any) => {
    let childRoute = menus.map((item: any) => {
        let route = {
            path: item.type == 'folder' ? '/' + item.path : item.path,
            name: item.componentName,
            component: () => import(`@/views${item.componentPath}`),
            redirect: item.redirect,
            meta: {
                name: item.name,
                icon: item.icon,
                type: item.type
            }
        }
        if (item.children && item.children.length > 0) {
            recursion(item.children, route)
        }
        return route
    })
    route.children = childRoute
}

router.beforeEach((to, from, next) => {
    if (to.name == "login") {
        next()
    } else {
        // 验证token
        let authorization = getAuthorization()
        if (!authorization) {
            message.error("无认证令牌")
            next({ name: 'login', replace: true }) // 登录页面直接放行
        }
        let st: any = store
        const userInfo = st && st.state.user.userInfo
        // 验证用户名
        if (!userInfo) {
            // 获取用户名
            store.dispatch("GET_USERINFO").then(() => {
                const menus = st && st.state.user.menus
                loadRouters(menus)
                next({ ...to, replace: true })
            })
        } else {
            next()
        }
    }
})

export default router
